package com.gitee.fanx.retty.web;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author fanhang
 */
public class PathUtils {

	private static final String SLASH = "/";

	public static String join(String... paths) {
		return Arrays.stream(paths).map(i -> standard(i)).collect(Collectors.joining());
	}

	private static String standard(String path) {
		if (StringUtils.isBlank(path)) {
			return "";
		}
		if (!path.startsWith(SLASH)) {
			path = SLASH + path;
		}
		if (path.endsWith(SLASH)) {
			path = path.substring(0, path.length() - 1);
		}
		return path;
	}

	public static Set<String> getFullPaths(String parentPath, String[] values, String[] paths) {
		int size = ArrayUtils.getLength(values) + ArrayUtils.getLength(paths);
		if (size == 0) {
			return Collections.emptySet();
		}
		Set<String> pathSet = new HashSet<>(size);
		if (values != null) {
			Arrays.stream(values).forEach(i -> pathSet.add(join(parentPath, i)));
		}
		if (paths != null) {
			Arrays.stream(paths).forEach(i -> pathSet.add(join(parentPath, i)));
		}
		return pathSet;
	}

}
